package com.link2room.rodimus.facade.impl;

import com.link2room.aeriabots.constant.WxQrcodeScanConstant;
import com.link2room.aeriabots.dto.param.base.WxScanCountDetailDto;
import com.link2room.aeriabots.dto.param.inside.WxFansInsideParamDto;
import com.link2room.aeriabots.dto.param.inside.WxScanCountDetailFindParamDto;
import com.link2room.aeriabots.dto.result.base.WxFansInsideResDto;
import com.link2room.blaster.dto.param.base.RegistrationVerificationBaseParamDto;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.param.base.BaseL2rParamDto;
import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.dto.result.base.BaseSession;
import com.link2room.common.dto.result.base.MemberSessionDto;
import com.link2room.common.dto.threadlocal.ApiThreadLocal;
import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.exception.SysException;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.mns.MNSUtil;
import com.link2room.common.redis.RedisUtil;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.*;
import com.link2room.enums.exception.RodimusSysExceptionEnum;
import com.link2room.mirage.dto.param.SessionRefreshParamDto;
import com.link2room.rodimus.constant.MemberConstant;
import com.link2room.rodimus.constant.RodimusMnsConstant;
import com.link2room.rodimus.constant.SaleMemberConstant;
import com.link2room.rodimus.dto.param.api.ModifyCurrentUserMemberNameParamDto;
import com.link2room.rodimus.dto.param.api.RodimusBindingApiParamDto;
import com.link2room.rodimus.dto.param.api.UsableCouopnsApiParamDto;
import com.link2room.rodimus.dto.param.base.RodimusLoginBaseParamDto;
import com.link2room.rodimus.entity.MemberBase;
import com.link2room.rodimus.entity.SalesmanBase;
import com.link2room.rodimus.entity.SalesmanMember;
import com.link2room.rodimus.facade.IMemberApiFacadeService;
import com.link2room.rodimus.feign.aeriabots.WxFansFeignService;
import com.link2room.rodimus.feign.balster.DayuVerificationMsgFeignService;
import com.link2room.rodimus.feign.mirage.MemberLoginFeignService;
import com.link2room.rodimus.feign.soundwave.PmsMemberFeignService;
import com.link2room.rodimus.listener.event.NewMemberEvent;
import com.link2room.rodimus.service.IMemberRankUtil;
import com.link2room.rodimus.service.IMemberService;
import com.link2room.soundwave.dto.param.inside.MemberLoginInsideParamDto;
import com.link2room.soundwave.dto.param.inside.UsableCouopnsInsideParamDto;
import com.link2room.soundwave.dto.result.base.*;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

import static com.link2room.enums.exception.RodimusSysExceptionEnum.*;

/**
 * Created by VegetaKo on 2016/10/17.
 */
@Service("memberApiFacadeService")
public class MemberApiFacadeServiceImpl implements IMemberApiFacadeService {

    private static Logger logger = LoggerFactory.getLogger(MemberApiFacadeServiceImpl.class);

    @Resource
    private ICommonService commonService;

    @Autowired
    private DayuVerificationMsgFeignService dayuVerificationMsgFeignService;

    @Autowired
    private WxFansFeignService wxFansFeignService;

    @Autowired
    private MemberLoginFeignService memberLoginFeignService;
    @Resource
    private RedisUtil redisUtil;

    @Resource
    private MNSUtil mnsUtil;

    @Autowired
    private PmsMemberFeignService pmsMemberFeignService;

    @Resource
    private IMemberRankUtil memberRankUtil;
    @Resource
    private IMemberService memberService;
    @Autowired
    private ApplicationEventPublisher publisher;

    @Override
    public void wxNewMember(RodimusLoginBaseParamDto rodimusLoginBaseParamDto) throws Exception {
        String lhotelGroupCode = rodimusLoginBaseParamDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode, "[lhotelGroupCode] is null");
        String mobile = rodimusLoginBaseParamDto.getMobile();
        L2RAssert.stringBlank(mobile, "[mobile] is null");
        String verifyCode = rodimusLoginBaseParamDto.getVerificationCode();
        L2RAssert.stringBlank(verifyCode, "[verifyCode] is null");
        checkverificationCode(lhotelGroupCode, mobile, verifyCode);


        //获取session
        MemberSessionDto memberSessionDto = ApiThreadLocal.getMemberSessionThreadLocal();

        String openid = memberSessionDto.getOpenid();
        rodimusLoginBaseParamDto.setWxOpenid(openid);
        String lhotelCode = getMemberSourceHotelCode(openid);
        rodimusLoginBaseParamDto.setLhotelCode(lhotelCode);
        MemberBase checkMemberBase = memberService.selectMemberByMobile(lhotelGroupCode, mobile);
        MemberBase salemanSeed;
        if (checkMemberBase != null) {
            //手机号存在时
            if (StringUtil.isNotBlank(checkMemberBase.getOpenid())) {
                //openid 不为null
                throw new SysException(RodimusSysExceptionEnum.MOBILE_EXIT);
            } else {
                //手机号重复，openid 没有时 更新，绑定openid
                MemberBase memberUpdate = ClassConverUtil.copyProperties(checkMemberBase, MemberBase.class);
                memberUpdate.setOpenid(openid);
                memberUpdate.setMemberName(rodimusLoginBaseParamDto.getMemberName());
                commonService.merge(memberUpdate);
                salemanSeed = memberUpdate;
            }
        } else {
            //手机号不重复的情况检测openid是否重复
            if (checkRepeatWxOpenid(lhotelGroupCode, openid)) {
                //重复更新
                MemberBase check = new MemberBase();
                check.setOpenid(openid);
                check.setLhotelGroupCode(lhotelGroupCode);
                MemberBase memberUpdate = commonService.findOneEQ(MemberBase.class, check);
                memberUpdate.setMobile(mobile);
                memberUpdate.setMemberName(rodimusLoginBaseParamDto.getMemberName());
                memberUpdate.setLhotelCode(lhotelCode);
                commonService.merge(memberUpdate);
                salemanSeed = memberUpdate;
                rodimusLoginBaseParamDto.setL2RMemberNo(salemanSeed.getMemberNo());
            } else {
                //不重复新建
                MemberBase memberBase = new MemberBase();
                memberBase.setMemberName(rodimusLoginBaseParamDto.getMemberName());
                memberBase.setMobile(rodimusLoginBaseParamDto.getMobile());
                memberBase.setLhotelGroupCode(lhotelGroupCode);
                memberBase.setMemberLevel(MemberConstant.MEMBER_LEVEL.L0);
                memberBase.setOpenid(openid);
                memberBase.setLhotelCode(lhotelCode);
                String memberNo = memberService.syncGetNewMemberNo();
                memberBase.setMemberNo(memberNo);
                rodimusLoginBaseParamDto.setL2RMemberNo(memberNo);
                commonService.save(memberBase);
                salemanSeed = memberBase;
            }
        }

        //更新session
        SessionRefreshParamDto refreshParam = new SessionRefreshParamDto();
        ClassConverUtil.copyProperties(salemanSeed, memberSessionDto);
        memberSessionDto.setIsMember(DefaultValue.T);
        refreshParam.setSession(memberSessionDto);
        memberLoginFeignService.refreshSession(refreshParam);
        //销售员更新或者新建
        String salesmanNo = salesManSaveOrUpdate(lhotelGroupCode, openid, salemanSeed.getMemberName(), salemanSeed.getMemberNo());
        rodimusLoginBaseParamDto.setSalesmanNo(salesmanNo);
        //通知PMS服务会员创建
        this.syncCreatePMSMember(rodimusLoginBaseParamDto);

        /**
         * 注册事件推送
         */
        publisher.publishEvent(new NewMemberEvent(lhotelGroupCode, salemanSeed.getMemberNo(), rodimusLoginBaseParamDto.getSalesmanNo(), rodimusLoginBaseParamDto.getFrom()));
    }

    /**
     * 异步创建pms会员
     *
     * @param paramDto
     */
    private void syncCreatePMSMember(RodimusLoginBaseParamDto paramDto) {
        mnsUtil.putMessageToQueue(RodimusMnsConstant.QUEUE_PMS_MEMBER, JacksonUtil.beanToJson(paramDto));
    }

    /**
     * 验证码检验
     *
     * @param lhotelGroupCode
     * @param mobile
     * @param verifyCode
     * @throws Exception
     */
    private void checkverificationCode(String lhotelGroupCode, String mobile, String verifyCode) throws Exception {
        RegistrationVerificationBaseParamDto registrationVerificationBaseParamDto = new RegistrationVerificationBaseParamDto();
        registrationVerificationBaseParamDto.setLhotelGroupCode(lhotelGroupCode);
        registrationVerificationBaseParamDto.setPhone(mobile);
        registrationVerificationBaseParamDto.setVerificationCode(verifyCode);
        //验证码
        dayuVerificationMsgFeignService.verificationCodes(registrationVerificationBaseParamDto);
    }

    /**
     * 更新或者新建销售员
     * PS:没有openid的时候直接return
     *
     * @param lhotelGroupCode
     * @param openid
     * @param memberName
     * @param memberNo
     * @throws Exception
     */
    private String salesManSaveOrUpdate(String lhotelGroupCode, String openid, String memberName, String memberNo) throws Exception {
        //没有openid的时候直接return
        if (StringUtil.isBlank(openid)) {
            return null;
        }
        WxFansInsideParamDto paramDto = new WxFansInsideParamDto();
        paramDto.setLhotelGroupCode(lhotelGroupCode);
        paramDto.setOpenid(openid);
        WxFansInsideResDto wxFansResDto = wxFansFeignService.getWxFans(paramDto.paramToMap()).getResultInfo();
        SalesmanMember salesmanMember = new SalesmanMember();
        if (null != wxFansResDto && StringUtil.isNotBlank(wxFansResDto.getSalesman())) {
            salesmanMember.setLhotelGroupCode(lhotelGroupCode);
            salesmanMember.setMemberWxopenid(wxFansResDto.getOpenid());
            salesmanMember = commonService.findOneEQ(SalesmanMember.class, salesmanMember);
            if (null != salesmanMember) {
                salesmanMember.setMemberName(memberName);
                salesmanMember.setMemberNo(memberNo);
                commonService.merge(salesmanMember);
            } else {
                salesmanMember = new SalesmanMember();
                salesmanMember.setMemberWxopenid(wxFansResDto.getOpenid());
                salesmanMember.setSalesmanNo(wxFansResDto.getSalesman());
                salesmanMember.setMemberName(memberName);
                salesmanMember.setMemberNo(memberNo);
                salesmanMember.setLhotelGroupCode(lhotelGroupCode);
                salesmanMember.setLhotelCode(DefaultValue.LHOTEL_CODE);
                salesmanMember.setSta(SaleMemberConstant.STA.I);
                salesmanMember.setAwardStarDatetime(new Date());
                salesmanMember.setAwardEndDatetime(DateUtil.addYears(new Date(), 1));
                commonService.save(salesmanMember);
            }
            return wxFansResDto.getSalesman();
        } else {
            return null;
        }
    }

    /**
     * 检查新建会员手机号是否重复,手机号重复时openid 不为为空时，重复
     *
     * @return
     */
    private boolean isMobileDuplicatedAndOpenidNotBlank(MemberBase check) throws Exception {
        return null != check && StringUtil.isNotBlank(check.getOpenid());
    }

    /**
     * 检查openid是否重复，1>若查询结果为0，返回true;2>若为1，返回false 3>若大于1则为openid重复抛出异常 4>没有openid则认为不重复
     *
     * @param lhotelGroupCode
     * @param wxOpenid
     * @return
     * @throws Exception
     */
    private boolean checkRepeatWxOpenid(String lhotelGroupCode, String wxOpenid) throws Exception {
        if (StringUtil.isBlank(wxOpenid)) {
            //没有openid则认为不重复
            return false;
        }
        MemberBase check = new MemberBase();
        check.setOpenid(wxOpenid);
        check.setLhotelGroupCode(lhotelGroupCode);
        QueryResult<MemberBase> list = commonService.findAllEQ(MemberBase.class, check);
        if (list.getTotalrecord() > 1) {
            throw new SysException(RodimusSysExceptionEnum.REPEAT_OPENID.getCode(), RodimusSysExceptionEnum.REPEAT_OPENID.getMsg() + ":" + wxOpenid, RodimusSysExceptionEnum.REPEAT_OPENID.getBizType());
        } else if (list.getTotalrecord() == 0) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public BaseSession getMember(RodimusLoginBaseParamDto rodimusLoginBaseParamDto) throws Exception {
        String lhotelGroupCode = rodimusLoginBaseParamDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode, "[lhotelGroupCode] is null");
        MemberSessionDto memberSessionDto = ApiThreadLocal.getMemberSessionThreadLocal();
        if (memberSessionDto == null) {
            throw new SysException(RodimusSysExceptionEnum.TIME_OUT);
        }
        if (!(memberSessionDto instanceof MemberSessionDto)) {
            return memberSessionDto;
        }
        return memberSessionDto;
    }


    /**
     * 更新联房会员信息
     * mysql and session
     *
     * @param pmsMemberInfoDto
     */
    private void updateL2RMemberEntityAndSession(PmsMemberInfoDto pmsMemberInfoDto) throws Exception {
        if (null != pmsMemberInfoDto) {
            //更新session
            MemberSessionDto memberSessionDto = ApiThreadLocal.getMemberSessionThreadLocal();
            String l2rMemberLevel = memberRankUtil.groupRankTOL2RRank(pmsMemberInfoDto.getLhotelGroupCode(), pmsMemberInfoDto.getCardType(), pmsMemberInfoDto.getCardLevel());//.gcRank2L2RRank(pmsMemberInfoDto.getCardLevel());
            //更新联房会员等级
            updateL2RMemberLevelEntity(memberSessionDto.getLhotelGroupCode(), memberSessionDto.getMemberNo(), l2rMemberLevel);

            //更新session
            SessionRefreshParamDto refreshParam = new SessionRefreshParamDto();
            memberSessionDto.setMemberLevel(l2rMemberLevel);
            memberSessionDto.setIsbindPms(MemberConstant.IS_PMSMEMBER.TRUE);
            refreshParam.setSession(memberSessionDto);
            memberLoginFeignService.refreshSession(refreshParam);

        }
    }

    private boolean isMemberBindPMS(String bindFlag) {
        return StringUtil.isNotBlank(bindFlag) && MemberConstant.IS_PMSMEMBER.TRUE.equals(bindFlag);
    }

    private MemberBase selectMemberByNo(String lhotelGroupCode, String memberNo) throws Exception {
        MemberBase querymb = new MemberBase();
        querymb.setLhotelGroupCode(lhotelGroupCode);
        querymb.setMemberNo(memberNo);
        return commonService.findOneEQ(MemberBase.class, querymb);
    }


    /**
     * 更新联房会员等级
     *
     * @param lhotelGroupCode
     * @param memberNo
     * @param l2rMemberLevel
     * @throws Exception
     */
    private void updateL2RMemberLevelEntity(String lhotelGroupCode, String memberNo, String l2rMemberLevel) throws Exception {
        MemberBase memberBase = selectMemberByNo(lhotelGroupCode, memberNo);
        if (memberBase == null) {
            return;
        }
        memberBase.setMemberLevel(l2rMemberLevel);
        memberBase.setIsbindPms(MemberConstant.IS_PMSMEMBER.TRUE);
        commonService.merge(memberBase);
    }

    @Override
    public void bindingPMSMember(RodimusBindingApiParamDto rodimusBindingApiParamDto) throws Exception {
        MemberSessionDto memberSessionDto = ApiThreadLocal.getMemberSessionThreadLocal();
        if (memberSessionDto == null) {
            return;
        }
        if (MemberConstant.IS_PMSMEMBER.TRUE.equals(memberSessionDto.getIsMember())) {
            String loginId = rodimusBindingApiParamDto.getLoginId();
            L2RAssert.stringBlank(loginId, "[loginId]用户名不能为空");
            String loginPasswd = rodimusBindingApiParamDto.getLoginPasswd();
            L2RAssert.stringBlank(loginPasswd, "[loginPasswd]密码不能为空");
            String memberNo = memberSessionDto.getMemberNo();
            L2RAssert.stringBlank(memberNo, "[memberNo] is null");
            //绑定实体卡
            MemberLoginInsideParamDto memberLoginInsideParamDto = new MemberLoginInsideParamDto();
            memberLoginInsideParamDto.setLoginId(loginId);
            memberLoginInsideParamDto.setLoginPasswd(loginPasswd);
            memberLoginInsideParamDto.setL2RMemberNo(memberNo);
            PmsMemberInfoDto pmsMemberInfoDto = pmsMemberFeignService.bindingPMSMember(memberLoginInsideParamDto.paramToMap()).getResultInfo();
            //更新联房会员等级
            updateL2RMemberEntityAndSession(pmsMemberInfoDto);
        }
    }

    @Override
    public void checkPMSMember(RodimusBindingApiParamDto rodimusBindingApiParamDto) throws Exception {
        String ticket = rodimusBindingApiParamDto.getTicket();
        if (StringUtil.isBlank(ticket)) {
            throw new SysException(BaseSysExceptionEnum.DATA_IS_NOT_EXIST);
        }
        MemberSessionDto memberSessionDto = (MemberSessionDto) redisUtil.get(ticket);
        if (memberSessionDto == null) {
            throw new SysException(RodimusSysExceptionEnum.TIME_OUT);
        }
        MemberLoginInsideParamDto memberLoginInsideParamDto = new MemberLoginInsideParamDto();
        ClassConverUtil.copyProperties(rodimusBindingApiParamDto, memberLoginInsideParamDto);
        memberLoginInsideParamDto.setL2RMemberNo(memberSessionDto.getMemberNo());
        PmsMemberInfoDto pmsMemberInfoDto = pmsMemberFeignService.checkPMSMember(memberLoginInsideParamDto.paramToMap()).getResultInfo();
        if (memberSessionDto.getMemberName().equals("微信粉丝") && null != pmsMemberInfoDto) {
            MemberBase memberBase = new MemberBase();
            memberBase.setMemberNo(memberSessionDto.getMemberNo());
            memberBase = commonService.findOneEQ(MemberBase.class, memberBase);
            String l2rMemberLevel = memberRankUtil.groupRankTOL2RRank(pmsMemberInfoDto.getLhotelGroupCode(), pmsMemberInfoDto.getCardType(), pmsMemberInfoDto.getCardLevel());//.gcRank2L2RRank(pmsMemberInfoDto.getCardLevel());
            memberBase.setMemberLevel(l2rMemberLevel);
            memberBase.setMemberName(pmsMemberInfoDto.getName());
            if (StringUtil.isBlank(memberBase.getMobile())) {
                memberBase.setMobile(pmsMemberInfoDto.getMobile());
            }
            commonService.merge(memberBase);
            SessionRefreshParamDto refreshParam = new SessionRefreshParamDto();
            refreshParam.setSession(ClassConverUtil.copyProperties(memberBase, MemberSessionDto.class));
            memberLoginFeignService.refreshSession(refreshParam);
        }
    }

    @Override
    public PmsMemberPointDto getPMSmemberPoints(RodimusLoginBaseParamDto rodimusLoginBaseParamDto) throws Exception {
        MemberSessionDto memberSession = ApiThreadLocal.getMemberSessionThreadLocal();
        MemberLoginInsideParamDto memberLoginParamDto = new MemberLoginInsideParamDto();
        memberLoginParamDto.setL2RMemberNo(memberSession.getMemberNo());
        PmsMemberPointDto pmsMemberPointDto = pmsMemberFeignService.getPMSMemberPoints(memberLoginParamDto.paramToMap()).getResultInfo();
        return pmsMemberPointDto;
    }

    @Override
    public QueryResult<PmsCardPointDto> getPMSmemberPointsDetail(RodimusLoginBaseParamDto rodimusLoginBaseParamDto) throws Exception {
        MemberSessionDto memberSession = ApiThreadLocal.getMemberSessionThreadLocal();
        MemberLoginInsideParamDto memberLoginParamDto = new MemberLoginInsideParamDto();
        memberLoginParamDto.setL2RMemberNo(memberSession.getMemberNo());
        QueryResult<PmsCardPointDto> cardPointDtoQueryResult = pmsMemberFeignService.getPMSMemberPointDetail(memberLoginParamDto.paramToMap()).getQueryResult();
        return cardPointDtoQueryResult;
    }

    @Override
    public QueryResult<PmsCouponDto> getPMSmemberCoupons(RodimusLoginBaseParamDto rodimusLoginBaseParamDto) throws Exception {
        MemberSessionDto memberSession = ApiThreadLocal.getMemberSessionThreadLocal();
        MemberLoginInsideParamDto memberLoginParamDto = new MemberLoginInsideParamDto();
        memberLoginParamDto.setL2RMemberNo(memberSession.getMemberNo());
        memberLoginParamDto.setCurrentPage(rodimusLoginBaseParamDto.getCurrentPage());
        memberLoginParamDto.setPageSize(rodimusLoginBaseParamDto.getPageSize());
        QueryResult<PmsCouponDto> memberCouponDtoQueryResult = pmsMemberFeignService.getPMSMemberCoupons(memberLoginParamDto.paramToMap()).getQueryResult();
        return memberCouponDtoQueryResult;

    }

    @Override
    public QueryResult<SalesmanBase> getSalesmans(RodimusLoginBaseParamDto rodimusLoginBaseParamDto) throws Exception {
        MemberSessionDto memberSessionDto = ApiThreadLocal.getMemberSessionThreadLocal();
        WxFansInsideParamDto paramDto = new WxFansInsideParamDto();
        paramDto.setLhotelGroupCode(rodimusLoginBaseParamDto.getLhotelGroupCode());
        paramDto.setOpenid(memberSessionDto.getOpenid());
        WxFansInsideResDto wxFansResDto = wxFansFeignService.getWxFans(paramDto.paramToMap()).getResultInfo();
        QueryResult<SalesmanBase> salesmanBaseQueryResult = new QueryResult<>();
        if (null != wxFansResDto && StringUtil.isNotBlank(wxFansResDto.getSalesman())) {
            SalesmanBase salesmanBase = new SalesmanBase();
            salesmanBase.setSalesmanNo(wxFansResDto.getSalesman());
            salesmanBase.setLhotelGroupCode(rodimusLoginBaseParamDto.getLhotelGroupCode());
            salesmanBaseQueryResult = commonService.findAllEQ(SalesmanBase.class, salesmanBase);
        }
        return salesmanBaseQueryResult;
    }

    @Override
    public QueryResult<PmsCouponDto> getUsableCoupons(UsableCouopnsApiParamDto usableCouopnsApiParamDto) throws Exception {
        MemberSessionDto memberSessionDto = ApiThreadLocal.getMemberSessionThreadLocal();
        UsableCouopnsInsideParamDto usableCouopnsInsideParamDto = new UsableCouopnsInsideParamDto();
        ClassConverUtil.copyProperties(usableCouopnsApiParamDto, usableCouopnsInsideParamDto);
        usableCouopnsInsideParamDto.setL2RMemberNo(memberSessionDto.getMemberNo());
        QueryResult<PmsCouponDto> pmsUsableCouponDtoQueryResult = pmsMemberFeignService.getUsableCoupons(usableCouopnsInsideParamDto.paramToMap()).getQueryResult();
        return pmsUsableCouponDtoQueryResult;
    }

    private String getMemberSourceHotelCode(String openid) throws Exception {
        //查询扫码记录.确定扫码的酒店
        WxScanCountDetailFindParamDto findWxQrCodeScanDetailParam = new WxScanCountDetailFindParamDto();
        findWxQrCodeScanDetailParam.setOpenid(openid);
        findWxQrCodeScanDetailParam.setScanType(WxQrcodeScanConstant.scan_type.SUBSCRIBE);
        findWxQrCodeScanDetailParam.setQrType(WxQrcodeScanConstant.qr_type.COMMERCIALS);
        findWxQrCodeScanDetailParam.setPageSize(1);
        findWxQrCodeScanDetailParam.setCurrentPage(0);
        BaseResultDto<WxScanCountDetailDto> res = wxFansFeignService.getWxScanCountDetail(findWxQrCodeScanDetailParam.paramToMap());
        if (CollectionUtils.isNotEmpty(res.getQueryResult().getResultlist())) {
            String lhotelCode = res.getQueryResult().getResultlist().get(0).getQrKey();
            return lhotelCode;
        } else {
            return DefaultValue.LHOTEL_CODE;
        }
    }

    @Override
    public void autoPMSMember(RodimusLoginBaseParamDto rodimusLoginBaseParamDto) throws Exception {
        //检查是直接匹配
        MemberLoginInsideParamDto memberLoginInsideParamDto = ClassConverUtil.copyProperties(rodimusLoginBaseParamDto, MemberLoginInsideParamDto.class);
        memberLoginInsideParamDto.setOpenid(rodimusLoginBaseParamDto.getWxOpenid());

        try {
            BaseResultDto baseResultDto = pmsMemberFeignService.autoPMSMember(memberLoginInsideParamDto);
            PmsMemberInfoDto pmsMemberInfoDto = (PmsMemberInfoDto) baseResultDto.getResultInfo();
            //更新当前会员等级
            updateL2RMemberEntityAndSession(pmsMemberInfoDto);
        } catch (SysException e) {
            //无法匹配会员 ，且无法注册成功会员
            MemberBase memberBase = new MemberBase();
            memberBase.setLhotelGroupCode(rodimusLoginBaseParamDto.getLhotelGroupCode());
            memberBase.setMemberNo(rodimusLoginBaseParamDto.getL2RMemberNo());
            memberBase = commonService.findOneEQ(MemberBase.class, memberBase);
            memberBase.setIsbindPms(MemberConstant.IS_PMSMEMBER.FALSE);
            commonService.merge(memberBase);
            throw e;
        }
    }

    @Override
    public PmsMemberInfoDto getPMSMemberInfo() throws Exception {
        MemberSessionDto memberSessionDto = ApiThreadLocal.getMemberSessionThreadLocal();
        if (DefaultValue.F.equals(memberSessionDto.getIsMember())) {
            throw new SysException(RodimusSysExceptionEnum.AUTHENTICATION_ERRO, memberSessionDto.getMemberNo() + "不是会员.");
        }
        MemberLoginInsideParamDto memberLoginInsideParamDto = new MemberLoginInsideParamDto();
        memberLoginInsideParamDto.setL2RMemberNo(memberSessionDto.getMemberNo());
        PmsMemberInfoDto pmsMemberInfoDto = pmsMemberFeignService.syncMember(memberLoginInsideParamDto.paramToMap()).getResultInfo();
        return pmsMemberInfoDto;
    }

    @Override
    public void syncCurrentMemberLevel(BaseL2rParamDto paramDto) throws Exception {
        MemberSessionDto memberSessionDto = ApiThreadLocal.getMemberSessionThreadLocal();
        String isMember = memberSessionDto.getIsMember();
        if (!MemberSessionDto.Member.equals(isMember)) {
        } else {

            MemberBase memberBase = selectMemberByNo(memberSessionDto.getLhotelGroupCode(), memberSessionDto.getMemberNo());
            L2RAssert.isTrue(null == memberBase, new SysException(MEMBER_SYNC, "会员不存在,[memberNo] is " + memberSessionDto.getMemberNo()));
            MemberLoginInsideParamDto memberLoginInsideParamDto = new MemberLoginInsideParamDto();
            memberLoginInsideParamDto.setMemberName(memberSessionDto.getMemberName());
            memberLoginInsideParamDto.setL2RMemberNo(memberSessionDto.getMemberNo());
            memberLoginInsideParamDto.setMobile(memberBase.getMobile());
            PmsMemberInfoDto pmsMemberInfoDto = pmsMemberFeignService.syncMember(memberLoginInsideParamDto.paramToMap()).getResultInfo();
            if (pmsMemberInfoDto != null) {
                String cardType = pmsMemberInfoDto.getCardType();
                String cardLevel = pmsMemberInfoDto.getCardLevel();
                if (StringUtil.isNotBlank(cardLevel) && StringUtil.isNotBlank(cardType)) {
                    String l2rLevel = memberRankUtil.groupRankTOL2RRank(pmsMemberInfoDto.getLhotelGroupCode(), cardType, cardLevel);
                    if (l2rLevel.equals(memberBase.getMemberLevel())) {
                        return;
                    }
                    if (DefaultValue.T.equals(pmsMemberInfoDto.getIsBindPms())) {
                        memberBase.setIsbindPms(DefaultValue.T);
                        memberSessionDto.setIsbindPms(DefaultValue.T);
                    } else if (DefaultValue.F.equals(pmsMemberInfoDto.getIsBindPms())) {
                        memberBase.setIsbindPms(DefaultValue.F);
                        memberSessionDto.setIsbindPms(DefaultValue.F);
                    }
                    memberBase.setMemberLevel(l2rLevel);
                    commonService.merge(memberBase);

                    SessionRefreshParamDto refreshParam = new SessionRefreshParamDto();
                    memberSessionDto.setMemberLevel(l2rLevel);
                    refreshParam.setSession(memberSessionDto);
                    memberLoginFeignService.refreshSession(refreshParam);
                }
            }
        }

    }

    @Override
    public void modifyCurrentUserMemberName(ModifyCurrentUserMemberNameParamDto paramDto) throws Exception {
        String memberName = paramDto.getMemberName();
        L2RAssert.isTrue(StringUtil.isBlank(memberName),new SysException(MODIFY_MEMBER_MEMBER_PARAM_INVALID));
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        MemberSessionDto memberSessionDto = ApiThreadLocal.getMemberSessionThreadLocal();
        boolean isMember = DefaultValue.T.equals(memberSessionDto.getIsMember());
        L2RAssert.isTrue(!isMember,new SysException(MODIFY_MEMBER_NAME_IS_NOT_MEMBER));
        String memberNo = memberSessionDto.getMemberNo();
        L2RAssert.isTrue(StringUtil.isBlank(memberNo),new SysException(MODIFY_MEMBER_MEMBER_NO_NOT_EXIT));
        MemberBase memberBase = selectMemberByNo(lhotelGroupCode, memberNo);
        L2RAssert.isTrue(null == memberBase,new SysException(MODIFY_MEMBER_MEMBER_NOT_EXIT));
        memberBase.setMemberName(memberName);
        commonService.merge(memberBase);
        //session刷新
        memberSessionDto.setMemberName(memberName);
        SessionRefreshParamDto refreshParam = new SessionRefreshParamDto();
        refreshParam.setSession(memberSessionDto);
        memberLoginFeignService.refreshSession(refreshParam);
    }
}
